<template>
  <li class="todo-item" :class="{ completed: isCompleted }">
    <div class="todo-checkbox" @click="handleToggle">
      <input 
        type="checkbox" 
        :checked="isCompleted"
      />
      <span></span>
    </div>
    <span class="todo-text" @click="handleToggle">{{ task.text }}</span>
    <button class="delete-btn" @click="deleteTask">删除</button>
  </li>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  task: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['toggle-task', 'delete-task'])

const isCompleted = ref(props.task.completed)

// 监听传入的task变化
watch(() => props.task.completed, (newValue) => {
  isCompleted.value = newValue
})

function handleToggle() {
  isCompleted.value = !isCompleted.value
  console.log('发送切换事件:', props.task.id)
  emit('toggle-task', props.task.id)
}

function deleteTask() {
  emit('delete-task', props.task.id)
}
</script>

<style scoped>
.todo-item {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  margin-bottom: 10px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.todo-item:hover {
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
}

.todo-item.completed .todo-text {
  text-decoration: line-through;
  color: #aaa;
}

.todo-checkbox {
  position: relative;
  margin-right: 12px;
  cursor: pointer;
}

.todo-checkbox input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 20px;
  width: 20px;
  z-index: 1;
}

.todo-checkbox span {
  position: relative;
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid #ddd;
  border-radius: 3px;
  transition: all 0.3s;
  background-color: white;
}

.todo-checkbox input:checked ~ span {
  background-color: #42b983;
  border-color: #42b983;
}

.todo-checkbox span:after {
  content: "";
  position: absolute;
  display: none;
  left: 5px;
  top: 1px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.todo-checkbox input:checked ~ span:after {
  display: block;
}

.todo-text {
  flex: 1;
  font-size: 16px;
  transition: all 0.3s;
}

.delete-btn {
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 6px 10px;
  cursor: pointer;
  transition: background 0.3s;
}

.delete-btn:hover {
  background: #ff5252;
}
</style> 